package com.qcby.controller;


import com.qcby.Constants;
import com.qcby.model.Actor;
import com.qcby.model.dto.ActorVo;
import com.qcby.model.resp.RespEntity;
import com.qcby.query.ActorQuery;
import com.qcby.service.ActorService;
import com.qcby.service.MovieActorRelationService;
import com.qcby.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//艺人信息管理
@RestController
@RequestMapping("/actor")
public class ActorController {
    @Autowired
    private ActorService actorService;
    @Autowired
    private MovieActorRelationService movieActorRelationService;

    /**
     * 艺人查询
     *
     * @param aq
     * @return
     */
    @RequestMapping("/list")
    public RespEntity<ActorVo> searchActor(@RequestBody ActorQuery aq) {
        if (aq != null) {
            if (aq.getPageNo() == null) {
                aq.setPageNo(1);
            }
            System.out.println();
            Page<Actor> page = actorService.selectObjectByCondition(aq, "0");
            System.out.println(page);
            return RespEntity.successCustomize(Constants.FLAG_200, "查询成功", new ActorVo(page, aq));
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "参数不能为空", null);
    }

    @RequestMapping("/listAll")
    public RespEntity<List<Actor>> listAll() {
        List<Actor> actorList = actorService.selectObjectAll();
        return RespEntity.successCustomize(Constants.FLAG_200, "查询成功", actorList);
    }

    /**
     * 点击查看详情  访问MovieActorRelationController中的方法!!
     */
    @RequestMapping("/addActor")
    public RespEntity<String> addActor(@RequestBody Actor actor) {
        if (actor != null) {
            actorService.insert(actor);
            return RespEntity.successCustomize(Constants.FLAG_200, "", "添加成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", "添加失败");
    }

    /**
     * 点击修改之后，根据aid获取艺人信息
     */
    @RequestMapping("/getActorById")
    public RespEntity<Actor> getActorById(Integer aid) {
        if (aid != null) {
            Actor actor = actorService.selectByPrimaryKey(aid);
            System.out.println(actor);
            return RespEntity.successCustomize(Constants.FLAG_200, "查询成功", actor);

        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "查询失败", null);
    }

    /**
     * 修改
     */
    @RequestMapping("/updateActor")
    public RespEntity<String> updateActor(@RequestBody Actor actor) {
        if (actor != null) {
            actorService.updateByPrimaryKeySelective(actor);
            return RespEntity.successCustomize(Constants.FLAG_200, "", "修改成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", "修改失败");
    }

    @RequestMapping("/deleteActor")
    public RespEntity<String> deleteActor(@RequestParam Integer aid) {
        if (aid != null) {
            actorService.deleteByPrimaryKey(aid);
            //实现关联表的删除操作！！！！
            movieActorRelationService.deleteByAid(aid);
            return RespEntity.successCustomize(Constants.FLAG_200, "", "删除成功");
        }
        return RespEntity.errorCustomize(Constants.FLAG_400, "", "删除失败");
    }
}
